using System;

namespace GameCore
{
    [Serializable]
    public struct ResourceID : IEquatable<ResourceID>
    {
        public string group;
        public string id;

        public ResourceID(string id)
        {
            group = "default";
            this.id = id;
        }
        
        public ResourceID(string group, string id)
        {
            this.group = group;
            this.id = id;
        }
        
        public bool Equals(ResourceID other)
            => string.Equals(group, other.group) && string.Equals(id, other.id);
        
        public override bool Equals(object obj)
            => obj is ResourceID other && Equals(other);
        
        public override int GetHashCode()
            => HashCode.Combine(group, id);

        public override string ToString()
        {
            return $"{group}:{id}";
        }
        
        public static bool operator ==(ResourceID left, ResourceID right) => left.Equals(right);
        public static bool operator !=(ResourceID left, ResourceID right) => !(left == right);

        public static implicit operator string(ResourceID res) => res.ToString();

        public static implicit operator ResourceID(string res)
        {
            var strs = res.Split(":");
            return strs.Length == 1 ? new ResourceID(strs[0]) : new ResourceID(strs[0], strs[1]);
        }
    }
}